package iotwisp.base.cqrs;

import iotwisp.base.rest.JSONResultDTO;
import org.springframework.http.HttpStatus;

import com.alibaba.fastjson.JSON;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@SuppressWarnings("serial")
public class BusinessException extends Exception {


    private String code;

	public final static String NECESSARY_PARAM_NULL = "necessary_param_null"; // 缺少必需的参数
	public final static HttpStatus NECESSARY_PARAM_NULL_HTTP_STATUS = HttpStatus.BAD_REQUEST; // 缺少必需的参数

	public final static String PARAM_ILLEGALITY = "param_illegality"; // 参数不合法
	public final static HttpStatus PARAM_ILLEGALITY_HTTP_STATUS = HttpStatus.BAD_REQUEST; // 参数不合法

	public final static String ENTITY_NOT_FOUND = "entity_not_found"; // 实体不存在
	public final static HttpStatus ENTITY_NOT_FOUND_HTTP_STATUS = HttpStatus.NOT_FOUND; // 实体不存在

	public final static String ENTITY_ID_REPEAT = "entity_id_repeat"; // 实体id重复
	public final static HttpStatus ENTITY_ID_REPEAT_HTTP_STATUS = HttpStatus.FORBIDDEN; // 实体id重复
	public final static String UNIQUE_RESTRICT = "unique_restrict"; // 唯一约束

	public final static String SYSTEM_ERROR = "system_error"; // 系统错误
	public final static String HANDLE_REPEAT = "handle_repeat"; // 请求重复

	public final static String STATUS_NOT_ALLOW = "status_not_allow";	//	当前状态不允许操作
	/** 树形操作异常 */
	public final static String PARENT_TYPE_NOT_FOUND = "parent_type_not_found"; // 父类型不存在
	public final static String CAN_NOT_MOVE_TO_CHILD = "can_not_move_to_child"; // 不能移动到下级节点之下

	public final static String RESOURCE_NOT_ENOUTH = "resource_not_enouth";

	private HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;

	public final static String NO_LOGIN = "no_login";
	public final static String LOGIN_EXIPRE = "login_expire";
	public final static String NO_PERMISSION = "no_permission";

	public BusinessException() {
		super();
	}

	public BusinessException(String message) {
		super(message);
	}

	public BusinessException(String code, String message) {
		super(message);
		this.code = code;
	}

	public static BusinessException getInstance(String code, String message) {
//		BusinessException businessException = new BusinessException(message);
//		businessException.setCode(code);
		JSONResultDTO jsonResultDTO = new JSONResultDTO();
		jsonResultDTO.setCode(code);
		jsonResultDTO.setMsg(message);
		String exJson = JSON.toJSONString(jsonResultDTO);
		BusinessException result = new BusinessException(exJson);
		return result;
	}

	public BusinessException(String code, String message, HttpStatus httpStatus) {
		super(message);
		this.code = code;
		this.httpStatus = httpStatus;
	}

}
